// VBConversions Note: VB project level imports
using System.Data;
using DevExpress.XtraEditors.Controls;
using System.Diagnostics;
using System.Xml.Linq;
using DevExpress.XtraEditors.Repository;
using System.Collections.Generic;
using System.Data.SqlClient;
using Microsoft.VisualBasic;
using System.Configuration;
using System.Collections;
using DevExpress.XtraEditors;
using System.Data.Sql;
using Microsoft.SqlServer;
using System.Windows.Forms;
using System;
using DevExpress.XtraReports;
using Microsoft.SqlServer.Management.Smo;
using DevExpress;
using System.Linq;
using System.IO;
// End of VB project level imports

using System.IO.Compression;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

/// <summary>
/// This module provide Extension for the DataTable and DataSet
/// </summary>
/// <remarks></remarks>
namespace SCIEF.SysBFW.CommonExt
{
	public static class DataExtension
	{
		/// <summary>
		/// Transform a DataTable to a GZIPed byte array
		/// </summary>
		/// <param name="dt"></param>
		/// <returns>Return a Gzip byte array </returns>
		/// <remarks>If an error occur, no exception is thrown but the Byte Array will be null</remarks>
		public static byte[] PushDataTable(this DataTable dt)
		{
			byte[] data = null;
			try
			{
				MemoryStream memStream = new MemoryStream();
				using (GZipStream zipStream = new GZipStream(memStream, CompressionMode.Compress))
				{
					dt.WriteXml(zipStream, XmlWriteMode.WriteSchema);
					zipStream.Close();
				}
				
				data = memStream.ToArray();
				memStream.Close();
			}
			catch
			{
				data = null;
			}
			return data;
		}
		
		
       
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="data"></param>
        /// <returns></returns>
		/// <remarks>If an error occur, no exception is thrown but the DataTable will be null</remarks>
	public static DataTable PullDataTable(this DataTable dt, byte[] data)
		{
        try
			{
				MemoryStream memStream = new MemoryStream(data);
				using (System.IO.Compression.GZipStream unzipStream = new System.IO.Compression.GZipStream(memStream, CompressionMode.Decompress))
				{
					dt.ReadXml(unzipStream);
					unzipStream.Close();
					memStream.Close();
				}
				
				memStream.Dispose();
			}
			catch (Exception)
			{
				dt = null;
			}
			return dt;
		}
		/// <summary>
		/// Transform a DataSet to a GZIPed byte array
		/// </summary>
		/// <param name="ds"></param>
		/// <returns></returns>
		/// <remarks>If an error occur, no exception is thrown but the Byte Array will be null</remarks>
		public static byte[] PushDataSet(this DataSet ds)
		{
			byte[] data = null;
			try
			{
				MemoryStream memStream = new MemoryStream();
				using (GZipStream zipStream = new GZipStream(memStream, CompressionMode.Compress))
				{
					ds.WriteXml(zipStream, XmlWriteMode.WriteSchema);
					zipStream.Close();
				}
				
				data = memStream.ToArray();
				memStream.Close();
			}
			catch
			{
				data = null;
			}
			return data;
		}
		/// <summary>
		/// Transform a GZiped byte array to a DataSet
		/// </summary>
		/// <param name="ds"></param>
    	/// <param name="data"></param>
        /// <returns></returns>
		/// <remarks>If an error occur, no exception is thrown but the Dataset will be null</remarks>
		public static DataSet PullDataSet(this DataSet ds, byte[] data)
		{
			try
			{
				using (MemoryStream memStream = new MemoryStream(data))
				{
					GZipStream unzipStream = new GZipStream(memStream, CompressionMode.Decompress);
					ds.ReadXml(unzipStream, XmlReadMode.ReadSchema);
					unzipStream.Close();
					memStream.Close();
				}
				
			}
			catch (Exception)
			{
				ds = null;
			}
			return ds;
		}
		/// <summary>
		/// Transform a DataTable to a GZIPed byte array
		/// </summary>
		/// <param name="dt"></param>
		/// <returns>Return a Gzip byte array </returns>
		/// <remarks>If an error occur, no exception is thrown but the Byte Array will be null</remarks>
		public static byte[] SerializeDataTable(this DataTable dt)
		{
			byte[] data = null;
			try
			{
				dt.RemotingFormat = SerializationFormat.Binary;
				MemoryStream memStream = new MemoryStream();
				BinaryFormatter bformat = new BinaryFormatter();
				using (GZipStream zipStream = new GZipStream(memStream, CompressionMode.Compress))
				{
					bformat.Serialize(zipStream, dt);
					zipStream.Close();
				}
				
				data = memStream.ToArray();
				memStream.Close();
			}
			catch (Exception)
			{
				data = null;
			}
			return data;
		}
		/// <summary>
		/// Transform a GZiped byte array to a DataTable
		/// </summary>
		/// <param name="data"></param>
        /// <param name="dt"></param>
		/// <returns></returns>
		/// <remarks>If an error occur, no exception is thrown but the DataTable will be null</remarks>
		public static DataTable DeSerializeDataTable(this DataTable dt, byte[] data)
		{
			try
			{
				MemoryStream memStream = new MemoryStream(data);
				BinaryFormatter bformat = new BinaryFormatter();
				using (System.IO.Compression.GZipStream unzipStream = new System.IO.Compression.GZipStream(memStream, CompressionMode.Decompress))
				{
					dt = (DataTable) (bformat.Deserialize(unzipStream));
					unzipStream.Close();
					memStream.Close();
				}
				
				memStream.Dispose();
			}
			catch (Exception)
			{
				dt = null;
			}
			return dt;
		}
		/// <summary>
		/// Transform a DataSet to a GZIPed byte array
		/// </summary>
		/// <param name="ds"></param>
		/// <returns></returns>
		/// <remarks>If an error occur, no exception is thrown but the Byte Array will be null</remarks>
		public static byte[] SerializeDataSet(this DataSet ds)
		{
			byte[] data = null;
			try
			{
				ds.RemotingFormat = SerializationFormat.Binary;
				MemoryStream memStream = new MemoryStream();
				BinaryFormatter bformat = new BinaryFormatter();
				using (GZipStream zipStream = new GZipStream(memStream, CompressionMode.Compress))
				{
					bformat.Serialize(zipStream, ds);
					zipStream.Close();
				}
				
				data = memStream.ToArray();
				memStream.Close();
			}
			catch (Exception)
			{
				data = null;
			}
			return data;
		}
		/// <summary>
		/// Transform a GZiped byte array to a DataSet
		/// </summary>
		/// <param name="data"></param>
        /// <param name="ds"></param>
        /// <returns></returns>
		/// <remarks>If an error occur, no exception is thrown but the Dataset will be null</remarks>
		public static DataSet DeSerializeDataSet(this DataSet ds, byte[] data)
		{
			try
			{
				using (MemoryStream memStream = new MemoryStream(data))
				{
					BinaryFormatter bformat = new BinaryFormatter();
					GZipStream unzipStream = new GZipStream(memStream, CompressionMode.Decompress);
					ds = (DataSet) (bformat.Deserialize(unzipStream));
					unzipStream.Close();
					memStream.Close();
				}
				
			}
			catch (Exception)
			{
				ds = null;
			}
			return ds;
		}
		
		/// <summary>
		///
		/// </summary>
		/// <param name="datarow"></param>
		/// <param name="columnName">ColumName</param>
		/// <param name="currentValue">Current Value in the Column</param>
		/// <param name="originalValue">Value before the change</param>
		/// <returns></returns>
		/// <remarks></remarks>
		public static bool IsColumnChanged(this DataRow datarow, string columnName, ref object currentValue, ref object originalValue)
		{
			bool hasChanged = false;
			originalValue = null;
			if (datarow.HasVersion(DataRowVersion.Current) && datarow.HasVersion(DataRowVersion.Original))
			{
				if (!datarow[columnName, DataRowVersion.Current].Equals(datarow[columnName, DataRowVersion.Original]))
				{
					hasChanged = true;
					originalValue = datarow[columnName, DataRowVersion.Original];
					currentValue = datarow[columnName, DataRowVersion.Current];
				}
			}
			else if (datarow.HasVersion(DataRowVersion.Current) && datarow.HasVersion(DataRowVersion.Original) == false)
			{
				if (!Microsoft.VisualBasic.Information.IsDBNull(datarow[columnName, DataRowVersion.Current]))
				{
					hasChanged = true;
					currentValue = datarow[columnName, DataRowVersion.Current];
				}
			}
			else if (datarow.HasVersion(DataRowVersion.Current) && datarow.HasVersion(DataRowVersion.Proposed) == false)
			{
                if (!Microsoft.VisualBasic.Information.IsDBNull(datarow[columnName, DataRowVersion.Current]))
				{
					hasChanged = true;
					currentValue = datarow[columnName, DataRowVersion.Current];
				}
			}
			return hasChanged;
		}
	}
	
}
